import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Form, Input, Button, Card, Typography, Space, message } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import styled from 'styled-components';
// 导入认证相关的 API 服务
import { authAPI } from '../services/api/auth';

const { Title, Text } = Typography;

const LoginContainer = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff 0%, #722ed1 100%);
`;

const LoginCard = styled(Card)`
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
`;

const Login: React.FC = () => {
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const handleLogin = async (values: { username: string; password: string }) => {
    try {
      setLoading(true);
      // 移除前端模拟的检查和延迟
      // 调用后端登录 API
      const response = await authAPI.login(values);

      if (response.success) {
        // 登录成功，authAPI.login 内部已经保存了 token
        message.success(response.message || '登录成功');
        // TODO: 根据实际需求，登录成功后可能需要获取用户信息或直接跳转到主页
        navigate('/main');
      } else {
        // 后端返回登录失败信息
        message.error(response.message || '登录失败，用户名或密码错误');
      }
    } catch (error: any) { // 捕获 API 请求错误
      console.error("Login API error:", error);
      // 根据错误类型显示不同信息，例如网络错误或后端返回的更详细错误
      message.error(error.message || '登录请求失败，请检查网络');
    } finally {
      setLoading(false);
    }
  };

  return (
    <LoginContainer>
      <LoginCard>
        <Space direction="vertical" size="large" style={{ width: '100%' }}>
          <div style={{ textAlign: 'center' }}>
            <Title level={2} style={{ marginBottom: 8 }}>区块链投票系统</Title>
            <Text type="secondary">请登录您的账号</Text>
          </div>
          
          <Form
            name="login"
            onFinish={handleLogin}
            layout="vertical"
            size="large"
          >
            <Form.Item
              name="username"
              rules={[{ required: true, message: '请输入用户名' }]}
            >
              <Input
                prefix={<UserOutlined />}
                placeholder="用户名"
                autoComplete="username"
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[{ required: true, message: '请输入密码' }]}
            >
              <Input.Password
                prefix={<LockOutlined />}
                placeholder="密码"
                autoComplete="current-password"
              />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                loading={loading}
                block
              >
                登录
              </Button>
            </Form.Item>

          </Form>
        </Space>
      </LoginCard>
    </LoginContainer>
  );
};

export default Login;